home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / encrypt.swg < prev    next >
Text File  |  1994-09-22  |  12KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00001                                                                           1      05-25-9408:18ALL                      JOHN FREESE              Xor Encryption/DecryptionSWAG9405            96     e├   (* $Header:   A:/vcs/mash.pav   1.0   15 Jul 1991  7:21:38   K_McCoy  $ *)π(****************************************************************************)π πUNIT MASH;π π{$IFDEF DOCUMENTATION}π π(****************************************************************************)π *  π *  $Log:   A:/vcs/mash.pav  $π * π *    Rev 1.0   15 Jul 1991  7:21:38   K_McCoyπ *    Added encryption unit, cleaned up.π * π *  π(****************************************************************************)π π{$V-}π π π                  Mash - The McCoy & Associates File Manglerπ π    Purpose:πππ    General purpose text file encrypter.  Keeps the honest people honest.π    Does simple XOR of text characters with user settable key.  Createsπ    a binary file with the extension .CRP containing the encrypted text.π    Binary files will be a little shorter than their ascii counterpartsπ    as there will be no CR/LF delimiters: only a length byte preceedingπ    each "line" of binary characters.  There is a 255 character limit onπ    the text files, but no limit on the number of lines (up to the capacityπ    of the disk).π π    Drawbacks:  This is nowhere near as secure as DES algorithm, althoughπ    it is much faster.  This unit won't keep the CIA / KGB out of yourπ    financial records for long, but it might work on your ex-wifeπ    and her laywer.π π    Note:  This unit uses the TPSTRING unit from Turbo Power.  You couldπ    easily change this to use the OPSTRING unit or write your ownπ    Trim [trailing blanks] function.π π    I wrote this for an automated test program to keep unauthorized peopleπ    from changing the test specifications out on the assembly line.π    (You wouldn't want airplanes falling on your head, would you?)π π    Suggested improvements:ππ    Allow other file extensions for input and output files.π    Modify this to work with binary input files.π    Modify the Mangle routine to not reset to the beginning of the key on eachπ    line of text.π π    Public Domain, but be nice and give me credit if you use it in yourπ    own stuff.π π    _Use at your own risk_  If the KGB de-encrypts your Mangled bowlingπ    scores, tough!ππ    Do an IO redirect of complaints to NUL.  Questions may be sent to:π π    Kevin McCoy - CompuServe ID# [72470,1233]π    2217 Aspenpark Ct.π    Thousand Oaks, CA 91362-1731π π π                                Sample Usage:πUSESπ    DOS,π    CRT,π    MASH;πVARπ    Strg,π    InName         : STRING;π    M              : Mangler;πBEGIN                             {main}π    ClrScr;π π    WRITE('Enter name of .CFG file: ');π    READLN(InName);π π    {I think this came from an old "Man from U.N.C.L.E." episode}π    M.SetSequence('OuR CaRs On IcE');ππ    {open the two files}π    IF NOT M.Init(InName, MASHMODE) THEN BEGINπ        WRITELN(M.MashError);π        HALT(1);π    END;π π    {encrypt the .CFG file}π    IF NOT M.MashFile THEN BEGINπ        WRITELN(M.MashError);π        HALT(1);π    END;ππ    {close the files}π    M.Done;π π    {Open just the encrypted file}π    IF NOT M.Init(InName, UNMASHMODE) THEN BEGINπ        WRITELN(M.MashError);π        HALT(1);π    END;π π    {read and decrypt each line until EOF}π    WHILE M.Getline(Strg) DOπ        WRITELN(Strg);π π    {close the .CRP file}π    M.Done;π πEND; {of sample program}π π π{$ENDIF}π{ The real stuff starts here... }π πINTERFACEππTYPEπ    MMType         = (MASHMODE, UNMASHMODE, SOURMASH);π π    Mangler        = OBJECTπ                         TFile          : TEXT;π                         BFile          : FILE;π                         LastError      : WORD;π                         FileName       : STRING;π                         InitMode       : MMType;π                         FUNCTION Init(Fname : STRING; Mode : MMType) :πBOOLEAN;π                         FUNCTION Getline(VAR Line : STRING) : BOOLEAN;π                         FUNCTION MashFile : BOOLEAN;π                         FUNCTION  MashError(VAR S : STRING) : BOOLEAN;π                         PROCEDURE SetSequence(Seq : STRING);π                         PROCEDURE Done;π                     END;π π   π(****************************************************************************)π πIMPLEMENTATIONππUSESπ    {.U-}π    TPSTRINGπ    {.U+}π    ;π πCONSTπ    {Default key.  May be reset with the SetSequence method}π    Id             : STRING =π'^%12hY7eujEDZ|R9a341~~#2DBC3fn7mSDVvUY@hbFD`6093fdk79*7a-|-  Q`';π π    {error number constants}π    INVINAM = 500;π    INVINIT = INVINAM + 1;π    CORRUPT = INVINIT + 1;π π π   π(****************************************************************************)π π    FUNCTION Mangle(L : STRING) : STRING;π        { Low budget encryption / decryption of Line}π    VARπ        I              : INTEGER;π    BEGINπ        FOR I := 1 TO LENGTH(L) DOπ            L[I] := CHR(ORD(L[I]) XOR NOT(ORD(Id[I MOD LENGTH(Id) + 1])));π        Mangle := L;π    END;π π   π(****************************************************************************)π π    FUNCTION Mangler.Init(Fname : STRING; Mode : MMType) : BOOLEAN;π        {- Gozintas:  Fname = Name (no extension) of the input/output files}π        {             Mode  = MASHMODE / UNMASHMODE (encrypt / decrypt)    }π        {  Gozoutas:  TRUE if everything was OK, FALSE if not              }π    VARπ        InName,π        OutName        : STRING;π    BEGINπ        InitMode := Mode;π        FileName := Fname;π        Init := TRUE;π        IF LENGTH(Trim(Fname)) = 0 THEN BEGINπ            LastError := INVINAM;π            Init := FALSE;π            EXIT;π        END;π        InName := Fname + '.CFG';π        OutName := Fname + '.CRP';π π        { Open the appropriate file(s) }π π        IF Mode = MASHMODE THEN BEGINπ            ASSIGN(TFile, InName); {open data files}π            {$I-}π            RESET(TFile);π            {$I+}π            LastError := IORESULT;π            IF LastError <> 0 THEN BEGINπ                {crash if file error}π                Init := FALSE;π                EXIT;π            END;π            ASSIGN(BFile, OutName);π            {$I-}π            REWRITE(BFile, 1);π            {$I+}π            LastError := IORESULT;π            IF LastError <> 0 THEN BEGINπ                {crash if file error}π                Init := FALSE;π                EXIT;π            END;π            Init := TRUE;π        ENDπ        ELSE BEGINπ            ASSIGN(BFile, OutName); {open data files}π            {$I-}π            RESET(BFile, 1);π            {$I+}π            LastError := IORESULT;π            IF LastError <> 0 THEN BEGINπ                {crash if file error}π                Init := FALSE;π                EXIT;π            END;π        END;π    END;π π   π(****************************************************************************)π π    FUNCTION Mangler.Getline(VAR Line : STRING) : BOOLEAN;π        {- Read a single line of binary gunk from the MSH file and decrypt it}π        {  Gozintas = Nothing                                                }π        {  Gozoutas:  Line = Decrypted ASCII string                          }π        {             Returns TRUE if everything was OK, FALSE if not        }π    VARπ        Result         : WORD;π    BEGINπ π        Line := '';π        Getline := FALSE;π π        IF InitMode <> UNMASHMODE THEN BEGINπ            LastError := INVINIT;π            EXIT;π        END;π π        BLOCKREAD(BFile, Line[0], 1, Result);π        IF Result <> 1 THEN BEGINπ            LastError := CORRUPT;π            EXIT;π        END;π π        BLOCKREAD(BFile, Line[1], ORD(Line[0]), Result);π        IF Result = ORD(Line[0]) THEN BEGINπ            Line := Mangle(Line);π            Getline := TRUE;π        ENDπ    END;π π   π(****************************************************************************)π π    FUNCTION WTOA(N : WORD; W : INTEGER) : STRING;π    VARπ        Strg           : STRING;π    BEGINπ        STR(N:W, Strg);π        WTOA := Strg;π    END;π π   π(******************************************************************************)π π    FUNCTION Mangler.MashError(VAR S : STRING) : BOOLEAN;π        {- return the last error string }π    BEGINπ        {most of these messages are unlikely to occur.  You may remove most }π        {of them to save memory }ππ        MashError := TRUE;π        CASE LastError OFπ            000 : BEGIN {no error}π                      S := '';π                      MashError := FALSE; π                  END;π            002 : S := 'File not found';π            003 : S := 'Path not found';π            004 : S := 'Too many open files';π            005 : S := 'File access denied';π            006 : S := 'Invalid file handle';π            012 : S := 'Invalid file access code';π            015 : S := 'Invalid drive number';π            016 : S := 'Cannot remove current directory';π            017 : S := 'Cannot rename across drives';π            100 : S := 'Disk read error';π            101 : S := 'Disk write error';π            102 : S := 'File not assigned';π            103 : S := 'File not open';π            104 : S := 'File not open for input';π            105 : S := 'File not open for output';π            150 : S := 'Disk is write-protected';π            151 : S := 'Unknown unit';π            152 : S := 'Drive not ready';π            154 : S := 'CRC error in data';π            156 : S := 'Disk seek error';π            157 : S := 'Unknown media type';π            158 : S := 'Sector not found';π            160 : S := 'Device write fault';π            161 : S := 'Device read fault';π            162 : S := 'Hardware failure';π            203 : S := 'Insufficient memory';π            INVINAM :S := 'Invalid filename';π            INVINIT :S := 'Invalid Mash unit init';π            CORRUPT : S := 'Invalid or corrupt MSH file';π            ELSEπ                S := 'Turbo runtime error ' + WTOA(LastError, 4);π        END;π    END;π π   π(****************************************************************************)π π    PROCEDURE Mangler.SetSequence(Seq : STRING);π        {- Set the encryption sequence (key) to be something other than theπdefault   }π        {  Gozintas: A string (the longer the better) containing any charactersπin    }π        {  the range of (0-255)  Try to avoid using strings that will beπduplicated   }π        {  in the text to be encrypted.  A match between the key and the textπresults }π        {  in strings of $FF characters in the MSH file that make the keyπeasier to   }π        {  crack by determined hackers                                         π      }π π    BEGINπ        Id := Seq;π    END;ππ   π(****************************************************************************)π π    FUNCTION Mangler.MashFile : BOOLEAN;π        {- File conversion method.  Encrypts text file specified in Init methodπcall }π        {  and places encrypted binary junk into the .MSH file                 π     }π        {  Returns TRUE if success, FALSE if not                               π     }π    VARπ        Strg           : STRING;π        Result         : WORD;π    BEGINπ        WHILE (NOT EOF(TFile)) DO BEGINπ            READLN(TFile, Strg);π            WRITELN(Strg);π            Strg := Mangle(Strg);π            BLOCKWRITE(BFile, Strg, ORD(Strg[0]) + 1, Result);π            IF Result <> LENGTH(Strg) + 1 THEN BEGINπ                WRITELN('Problem writing ' + FileName + '.MSH');π                HALT(1);π            END;π        END;                      {while}π π    END;π π   π(****************************************************************************)π π    PROCEDURE Mangler.Done;π        {- Close up shop and boogie method}π    BEGINπ        CLOSE(BFile);π        IF InitMode = MASHMODE THENπ            CLOSE(TFile);π π        InitMode := SOURMASH;π    END;π π   π(****************************************************************************)π    {unit initialization}πEND.                              {of unit mash}π(****************************************************************************)π(****************************************************************************)π